package com.lwisn.demo.filesystem.dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EncodingDaoImpl implements EncodingDao {

	private final Logger logger = LoggerFactory
			.getLogger(EncodingDaoImpl.class);

	String writePath = "\\development\\MavenSpringDemo\\encodedText";
	String extension = ".txt";

	public void writeToFileSystem(CharSequence charSequence, Charset encoding) {
		File encodedFile = new File(writePath + extension);
		try {
			logger.debug("Writing file in [{}] format.", encoding);
			FileOutputStream outputStream = FileUtils
					.openOutputStream(encodedFile);
			FileUtils.write(encodedFile, charSequence, encoding, true);
			// FileOutputStream outputStream = new
			// FileOutputStream(encodedFile);
			logger.debug("Wrote file to [{}]", encodedFile.getCanonicalPath());
			outputStream.close();

		} catch (FileNotFoundException e) {
			logger.error("File could not be found in specified location.");
		} catch (IOException e) {
			logger.error("File could not be written to filesystem.");
		}
	}

}
